#!/bin/bash

check_service() {
    service_name=$1
    port=$2
    check_command=$3

    echo "=== 检查 $service_name ==="

    # 检查端口
    if netstat -tln | grep ":$port " > /dev/null; then
        echo "✅ 端口 $port 监听正常"
    else
        echo "❌ 端口 $port 未监听"
        return 1
    fi

    # 检查进程
    if pgrep -f $service_name > /dev/null; then
        echo "✅ 进程存在"
    else
        echo "❌ 进程不存在"
        return 1
    fi

    # 执行特定检查命令
    if eval $check_command; then
        echo "✅ 服务检查正常"
    else
        echo "❌ 服务检查失败"
        return 1
    fi

    echo ""
}

# 检查RabbitMQ
check_service "rabbitmq" "5672" "rabbitmqctl ping"

# 检查Pulsar
check_service "pulsar" "6650" "curl -s http://localhost:8080/admin/v2/clusters"

# 检查Kafka
check_service "kafka" "9092" "kafka-topics.sh --list --bootstrap-server localhost:9092 > /dev/null 2>&1"
